<?php

class text
{

  /**
  @function str2URL

  Transforms a string to a proper URL.

  @param str			string		String to transform
  @param with_slashes		boolean		Keep slashes in URL
  @return string
  */
  public static function str2URL($str,$with_slashes=true)
  {
    $pattern['A'] = '\x{00C0}-\x{00C5}';
    $pattern['AE'] = '\x{00C6}';
    $pattern['C'] = '\x{00C7}';
    $pattern['D'] = '\x{00D0}';
    $pattern['E'] = '\x{00C8}-\x{00CB}';
    $pattern['I'] = '\x{00CC}-\x{00CF}';
    $pattern['N'] = '\x{00D1}';
    $pattern['O'] = '\x{00D2}-\x{00D6}\x{00D8}';
    $pattern['OE'] = '\x{0152}';
    $pattern['S'] = '\x{0160}';
    $pattern['U'] = '\x{00D9}-\x{00DC}';
    $pattern['Y'] = '\x{00DD}';
    $pattern['Z'] = '\x{017D}';

    $pattern['a'] = '\x{00E0}-\x{00E5}';
    $pattern['ae'] = '\x{00E6}';
    $pattern['c'] = '\x{00E7}';
    $pattern['d'] = '\x{00F0}';
    $pattern['e'] = '\x{00E8}-\x{00EB}';
    $pattern['i'] = '\x{00EC}-\x{00EF}';
    $pattern['n'] = '\x{00F1}';
    $pattern['o'] = '\x{00F2}-\x{00F6}\x{00F8}';
    $pattern['oe'] = '\x{0153}';
    $pattern['s'] = '\x{0161}';
    $pattern['u'] = '\x{00F9}-\x{00FC}';
    $pattern['y'] = '\x{00FD}\x{00FF}';
    $pattern['z'] = '\x{017E}';

    $pattern['ss'] = '\x{00DF}';

    foreach ($pattern as $r => $p) {
      $str = preg_replace('/['.$p.']/u',$r,$str);
    }

    $str = preg_replace('/[^A-Za-z0-9_\s\'\:\/[\]-]/','',$str);

    return self::tidyURL($str,$with_slashes);
  }

  /**
  @function tidyURL

  Cleans an URL.

  @param str			string		URL to tidy
  @param keep_slashes		boolean		Keep slashes in URL
  @param keep_spaces		boolean		Keep spaces in URL
  @return string
  */
  public static function tidyURL($str,$keep_slashes=true,$keep_spaces=false)
  {
    $str = strip_tags($str);
    $str = str_replace(array('?','&','#','=','+','<','>'),'',$str);
    $str = str_replace("'",'',$str);
    $str = preg_replace('/[\s]+/',' ',trim($str));

    if (!$keep_slashes) {
      $str = str_replace('/','-',$str);
    }

    if (!$keep_spaces) {
      $str = str_replace(' ','-',$str);
    }

    $str = preg_replace('/[-]+/','-',$str);

    # Remove path changes in URL
    $str = preg_replace('%^/%','',$str);
    $str = preg_replace('%\.+/%','',$str);

    return ucfirst(strtolower($str));
  }

}
?>